<template>
  <div class="login-container">
    <div class="form">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="demo-ruleForm">
        <el-form-item prop="username">
          <el-input type="text" prefix-icon="el-icon-user" placeholder="用户名" v-model="ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" prefix-icon="el-icon-lock" placeholder="密码" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="submit-button" @click="submitForm('ruleForm')">提交</el-button>
<!--          <el-row>-->
<!--            <el-col :span=10 style="text-align: right">-->
<!--              <el-button @click="resetForm('ruleForm')">重置</el-button>-->
<!--            </el-col>-->
<!--            <el-col :span=10 :offset=4>-->
<!--              <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>-->
<!--            </el-col>-->
<!--          </el-row>-->
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>

  import { getTokenModel } from '@/model/loginModel'
  import { getLogin } from '@/utils/login'
  import createRoutes,{LayoutIndex} from '@/utils/createRoutes'


  export default {
    name: "Index",
    data() {
      const validateUsername = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入用户名'));
        } else {
          callback();
        }
      };
      const validatePassword = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          username: '',
          password: '',
        },
        rules: {
          username: [
            { validator: validateUsername, trigger: 'blur' }
          ],
          password: [
            { validator: validatePassword, trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
      submitForm(formName){
        this.$refs[formName].validate((valid) => {
          const value = {
            username:this.ruleForm.username,
            password:this.ruleForm.password
          }
          if (valid) {
            // alert('submit!');
            getTokenModel({
              payload:value,
              callback:res=>{
                sessionStorage.setItem('loginInfo',JSON.stringify(res.data))
                this.$store.dispatch('menu/getMenuAction').then(()=>{
                  const childRoutes = createRoutes(this.$store.state.menu.menuList)
                  const routes = [{
                    path: '/back',
                    name: 'LayoutIndex',
                    component: LayoutIndex,
                    children:[]
                  }]
                  childRoutes.map(item=>{
                    routes[0].children.push(item)
                  })
                  this.$router.addRoutes(routes)
                }).then((routes)=>{
                  this.$router.push('/back/sys')
                })

              }
            })

          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style lang="stylus" scoped>
  .login-container
    width 100%
    height 100%
    padding-top 200px
    background-image url("../../assets/login_back.jpg")
    background-repeat no-repeat
    background-size 100% 100%
    .form
      background-color #ffffff
      width 400px
      height 260px
      margin 0 auto
      padding 55px 20px 5px 20px
      box-sizing border-box
      border-radius 10px
      .submit-button
        width 100%
</style>